const dateTimePicker = require('./dateTimePicker.js');
const { showToast } = getApp()
const {
  FormatDate
} = require('../../../utils/FormDate');
Component({
  properties: {
    placeholder: {
      type: String,
      value: '请输入'
    },
    title: String,
    required: Boolean,
    disabled: Boolean,
    readonly: Boolean,
    inset: Boolean,
    startTitle: {
      type: String,
      value: '开始日期'
    },
    endTitle: {
      type: String,
      value: '结束日期'
    },
    startPlaceholder: {
      type: String,
      value: '请选择',
    },
    endPlaceholder: {
      type: String,
      value: '请选择',
    },
    value: {
      type: null,
      value: []
    },
    showClearBtn: {
      type: Boolean,
      value: true
    }
  },
  observers: {
    'value': function (value) {
      if (value) {
        this.setData({
          startValue: value[0],
          endValue: value[1]
        })
      } else {
        this.setData({
          value: []
        })
      }
    }
  },
  data: {
    dateTimeArray: [],
    currentStartValue: '',
    currentEndValue: '',
    endValue: '',
    startValue: '',
  },
  lifetimes: {
    attached() {
      const currentYear = new Date().getFullYear()
      const array = dateTimePicker.dateTimePicker(currentYear - 2, currentYear + 5)
      this.setData({
        dateTimeArray: array.dateTimeArray
      });
    }
  },

  methods: {
    startChangeDateTime(event) {
      const value = this.getValue(event.detail.value)
      this.setData({
        startValue: value,
        currentIndex: 0
      })

      this.bindChange();
    },
    endChangeDateTime(event) {
      const value = this.getValue(event.detail.value)
      this.setData({
        endValue: value,
        currentIndex: 1
      })

      this.bindChange();
    },

    getValue(value) {
      const date = this.data.dateTimeArray[0][value[0]]
      const hour = this.data.dateTimeArray[1][value[1]]
      const minute = this.data.dateTimeArray[2][value[2]]
      const result = `${date} ${hour}:${minute}`
      return result;
    },
    onStartClick() {
      let value = new FormatDate().format('yyyy-MM-dd hh:mm');

      if (this.data.startValue) {
        value = this.data.startValue
      }
      const currentValue = value.replace(':', ' ').split(' ');
      const valueArray = this.data.dateTimeArray.map((item, index) => {
        return item.findIndex(f => f + '' === currentValue[index] + '');
      })

      this.setData({
        currentStartValue: valueArray
      })
    },

    onEndClick() {
      let value = new FormatDate().format('yyyy-MM-dd hh:mm');

      if (this.data.startValue) {
        value = this.data.startValue
      }
      const currentValue = value.replace(':', ' ').split(' ');
      const valueArray = this.data.dateTimeArray.map((item, index) => {
        return item.findIndex(f => f + '' === currentValue[index] + '');
      })

      this.setData({
        currentEndValue: valueArray
      })
    },

    onStartClear() {
      this.setData({
        startValue: ''
      })

      this.bindChange();
    },

    onEndClear() {
      this.setData({
        endValue: ''
      })

      this.bindChange();
    },

    bindChange() {
      const { startValue, endValue } = this.data;
      let startTime = '';
      let endTime = '';
      if (startValue) {
        const list = startValue.replace(' ', '-').split(':').join('-').split('-');
        startTime = new Date(...list).getTime()
      }

      if (endValue) {
        const list = endValue.replace(' ', '-').split(':').join('-').split('-');
        endTime = new Date(...list).getTime()
      }

      if (startTime && endTime) {
        if (endTime < startTime) {
          showToast('结束时间不能早于开始时间');
          const currentIndex = this.data.currentIndex

          if (currentIndex === 0) {
            this.setData({
              startValue: ''
            })
          } else {
            this.setData({
              endValue: ''
            })
          }
          return;
        }
      }
      this.triggerEvent('change', [startValue, endValue])
    }
  }
})